#ifndef _TMP_H_
#define _TMP_H_


#include <iostream>

#if 0
//lazy 时间换空间
class singleton{

private:
	singleton();
	static singleton * m_instance;
public:
	static singleton* getInstance();
	~singleton();
	static int i;
};


int singleton::i = 0;
singleton * singleton::m_instance = NULL;

singleton::singleton()
{
	std::cout << "constructor " << i++ << std::endl;
}

singleton::~singleton()
{
	std::cout << "destructor " << i++ << std::endl;
	delete getInstance();
	m_instance = NULL;
}

singleton* singleton::getInstance()
{
	std::cout << "get1 " << i++ << std::endl;		
	if(m_instance == NULL){
		std::cout << "get2 " << i++ << std::endl;			
		m_instance = new singleton();
	}
	return m_instance;
}
#endif



//lazy 2 时间换空间
class singleton{

private:
	singleton();
	static singleton * m_instance;
	
	
	
	//static void del(); //no use
public:
	static singleton* getInstance();
	~singleton();
	static int i;


private:
	class GarBo{
	public:
		~GarBo(){
			std::cout << "GarBo1 destructor" << i++ << std::endl;	
			if(singleton::m_instance){
				std::cout << "GarBo2 destructor" << i++ << std::endl;	
				delete singleton::m_instance;
			}
		}
	};
	static GarBo garBo;
};


int singleton::i = 0;
singleton * singleton::m_instance = NULL;
//singleton::GarBo (singleton::garBo) = 0;

singleton::singleton()
{
	std::cout << "constructor " << i++ << std::endl;
}

singleton::~singleton()
{
	std::cout << "destructor " << i++ << std::endl;
}

singleton* singleton::getInstance()
{
	std::cout << "get1 " << i++ << std::endl;		
	if(m_instance == NULL){
		std::cout << "get2 " << i++ << std::endl;			
		m_instance = new singleton();
	}
	return m_instance;
}
#if 0
//move out to in class
singleton::GarBo::~GarBo()
{
	std::cout << "GarBo1 " << i++ << std::endl;	
	if(singleton::m_instance){
		std::cout << "GarBo2 " << i++ << std::endl;	
		delete singleton::m_instance;
	}
}
#endif
#if 0 
//no use
void singleton::del()
{
	std::cout << "del1 " << i++ << std::endl;	
	if(singleton::m_instance){
		std::cout << "del2 " << i++ << std::endl;	
		delete singleton::m_instance;
	}
}
#endif




#if 0
//starve 空间换时间
class singleton{

private:
	singleton();
	static singleton * m_instance;
public:
	static singleton* getInstance();
	~singleton();
	static int i;
};


int singleton::i = 0;
singleton * singleton::m_instance = new singleton();

singleton::singleton()
{
	std::cout << "constructor " << i++ << std::endl;
}

singleton::~singleton()
{
	std::cout << "destructor " << i++ << std::endl;
}

singleton* singleton::getInstance()
{
	std::cout << "get1 " << i++ << std::endl;
	return m_instance;
}
#endif




//嵌套类中嵌套类
class A{
public:
	~A()
	{
		std::cout << "destructor A " << std::endl;		
	}
	class B{
	public:
		~B(){
			std::cout << "destructor B " << std::endl;			
		}
	};
	//static B b; //destructor fail
	B b;//destructor ok 
};


#endif